package pl.nazaweb.imedproplus.rest;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import pl.nazaweb.imedproplus.business.service.StaffService;
import pl.nazaweb.imedproplus.rest.pojo.ResponsePojo;
import pl.nazaweb.imedproplus.rest.pojo.StaffPojo;

@Stateless
@Path("/json/staff")
public class Staff extends AbstractRest<StaffPojo> {
    
    @EJB
    private StaffService staffService;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponsePojo getStaffList() {
        return this.getEntityPojoList();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/create")
    public ResponsePojo createStaffs(List<StaffPojo> staffPojos) {
        return this.createEntities(staffPojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/destroy")
    public ResponsePojo removeStaffs(List<StaffPojo> staffPojos) {
        return this.destroyEntities(staffPojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/update")
    public ResponsePojo updateUsers(List<StaffPojo> staffPojos) {
        return this.updateEntities(staffPojos);
    }

    @Override
    public List<StaffPojo> getEntityPojoListResult() {
        List<StaffPojo> result = new ArrayList<StaffPojo>();
        for (pl.nazaweb.imedproplus.model.entity.Staff staff : this.staffService.getStaffList()) {
            result.add(new StaffPojo(staff));
        }
        return result;
    }

    @Override
    public void persistNewEnities(List<StaffPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Staff> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Staff>();
        for (StaffPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.staffService.addNewStaffs(result);
    }

    @Override
    public void removeEnities(List<StaffPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Staff> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Staff>();
        for (StaffPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.staffService.removeStaffs(result);
    }

    @Override
    public void updateEnities(List<StaffPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Staff> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Staff>();
        for (StaffPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.staffService.updateStaffs(result);
    }
}
